package com.hb.controller.approve;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hb.model.Result;
import com.hb.pojo.dto.approve.ApproveDto;
import com.hb.pojo.po.approve.Approve;
import com.hb.pojo.po.leave.Leave;
import com.hb.pojo.po.role.Role;
import com.hb.pojo.vo.approve.ApproveVo;
import com.hb.pojo.vo.leave.LeaveVo;
import com.hb.pojo.vo.validated.GroupA;
import com.hb.pojo.vo.validated.GroupD;
import com.hb.service.approve.ApproveService;
import com.hb.service.leave.LeaveService;
import com.hb.utils.LocalDateTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.log4j.Log4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Api(tags = "Approve Related interfaces")
@Transactional
@Validated
@RestController
@RequestMapping("approve")
@Log4j
public class ApproveController {

	@Resource
	private ApproveService approveService;
	@Resource
	private LeaveService leaveService;

	/**
	 * approve a leave
	 */
	@ApiIgnore
	@ApiOperation(value = "approve a leave", notes = "approve a leave(request Json)")
	@PostMapping()
	public Result<Approve> insertSelectiveOne(@RequestBody @Parameter(name = "添加对象") @Validated(GroupA.class) ApproveDto approveDto) throws ParseException {
		Role role = (Role)  SecurityUtils.getSubject().getPrincipal();
		String username=role.getRole();
		approveDto.setApprove_role(username);
		Approve approve = approveService.insertSelective(approveDto);

		Leave leave=leaveService.selectByPrimaryKey(approveDto.getLeave_id());

		long betweenDays= LocalDateTimeUtil.betweenTime(leave.getBegin_date(),leave.getEnd_date());

		LeaveVo leaveVo=new LeaveVo();
		if(approveDto.getApprove_status()!=-1) {
			if (username.equals("manager")) {
				if (betweenDays > 10) {
					leaveVo.setStatus(1);
				} else {
					leaveVo.setStatus(2);
				}
			} else if (username.equals("senior manager")) {
				leaveVo.setStatus(2);
			}
		}else{
			leaveVo.setStatus(-1);
		}
		leaveVo.setLeave_id(approveDto.getLeave_id());
		leaveService.updateByPrimaryKey(leaveVo);
		return approve != null ? Result.success(approve) : Result.error();
	}


	/**
	 * get leave approve list record
	 */
	@ApiIgnore
	@ApiOperation(value = "query list", notes = "get leave approve list record")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNumber", value = "pages count", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "record per page", paramType = "query", required = true, dataType = "String"),
	})
	@GetMapping()
	public Result<ApproveDto> getList(@ApiIgnore @Validated(GroupD.class) ApproveVo approveVo) {
		PageMethod.startPage(approveVo.getPageNumber(), approveVo.getPageSize());
		List<ApproveDto> list = approveService.selectByVo(approveVo);
		PageInfo<ApproveDto> pageInfo = new PageInfo<>(list);
		return Result.success(pageInfo);
	}

}
